<?php

require('pagina.php');
require('database.php');
require('auth.php');
require('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Locations statistics');
adicionar_css('statistics.css');
pagina_head();

$location_id = (isset($_GET['location'])? $_GET['location'] : 0);
$year = (isset($_GET['year'])? $_GET['year'] : date("Y"));
$month = (isset($_GET['month'])? $_GET['month'] : 0);

// Pega o nome e id de todas as locacoes
$locacoes = array();
$resultado = query_bd("SELECT locations.id,  contacts.name FROM locations, contacts " .
	"WHERE locations.locationcontact=contacts.id AND locations.operating=TRUE ORDER BY contacts.name");
while ($loc = mysql_fetch_array($resultado))
	$locacoes[] = $loc;

if ($location_id > 0)
{
	// Pega nome da locacao especifica
	$location_name = query_fetch("SELECT contacts.name FROM locations, contacts " .
		"WHERE locations.locationcontact=contacts.id AND locations.operating=TRUE " .
        "AND locations.id={$location_id} LIMIT 1");
}

// Mostra opcoes
echo "<ul id=opcoes>";
echo "<li><a href='statistics.php?",
    (isset($_GET['year'])? "&amp;year={$_GET['year']}" : ""),
    (@$_GET['location']? "&amp;location=" . $_GET['location'] : ""),
    "&amp;print=1' target='_blank'><img src='img/print.png' alt=''>Print</a></li>";
if ($month == 0)
    echo "<li><a href='statistics_year_table.php?",
        (isset($_GET['year'])? "&amp;year={$_GET['year']}" : ""),
        (@$_GET['location']? "&amp;location=" . $_GET['location'] : ""),
        "' target='_blank'><img src='img/excel.png' alt=''>Excel Table</a></li>";
else
    echo "<li><a href='statistics_month_table.php?",
        (isset($_GET['year'])? "&amp;year={$_GET['year']}" : ""),
        (isset($_GET['month'])? "&amp;month={$_GET['month']}" : ""),
        (@$_GET['location']? "&amp;location=" . $_GET['location'] : ""),
        "' target='_blank'><img src='img/excel.png' alt=''>Excel Table</a></li>";
echo "</ul><br><br>";

/* Form to input location, year and month */
echo "<form id=periodo method=GET action=statistics.php>";

// Locacao
echo "<label for=location>Location</label>: <select name=location id=location>";
echo "<option value='-2' ", (@$_GET['location'] == -2? "selected=TRUE " : ""), 
     ">Every Location(Location by Location stat)</option>";
echo "<option value='-1' ", (@$_GET['location'] == -1? "selected=TRUE " : ""), 
     ">Any Location(All incomes and expenses)</option>";
echo "<option value=0 ", (@$_GET['location'] == 0? "selected=TRUE " : ""), 
     ">All Locations(Commom incomes and expenses)</option>";
foreach ($locacoes as $loc)
	echo "<option value={$loc['id']}", 
         (@$_GET['location'] == $loc['id']? " selected=TRUE" : ""), 
         ">{$loc['name']}</option>";

echo "</select>";

// Ano
echo "<label for=year>Year</label>: <input id=year name=year type=TEXT value='{$year}' onChange='format_int(this)' size=4>";

/* Month */
echo "<label for=month>Month</label>: <select name=month id=month>";
echo "<option value=0 ", ($month == 0? "selected=TRUE " : ""), ">All(Year stat)</option>";
for ($n=1; $n<=12; $n++)
    echo "<option value={$n} ", ($month == $n? "selected=TRUE " : ""), 
         ">{$MONTH_NAMES[$n-1]}</option>";
echo "</select>";

echo "<br><input type='Submit' value='Submit'>";
echo "</form>";

flush();
echo "<div id=table_container>";

/* 
    Decide which table to use: every location,
    location by year or location by month
*/
if (@$_GET['location'] == -2)
    include('statistics_every_table.php');
else if ($month == 0)
    include('statistics_year_table.php');
else
    include('statistics_month_table.php');
echo "</div>";

unset($locacoes);

echo "<div id=charts>";
echo "<p><a href='javascript:void()' onclick='javascript:document.getElementById(\"charts\").innerHTML=\"";

if (sizeof($chart_pos) > 0)
{
    asort($chart_pos, SORT_NUMERIC);
    $chart_max = max($chart_pos);

    /* scale values from 0 to 100 */
    foreach ($chart_pos as &$value)
        $value *= 100 / $chart_max;

    /* shows pie chart image */
    echo "<img src=\\\"https://chart.googleapis.com/chart?chs=750x400&chd=t:",
         implode(',',$chart_pos), "&cht=p&chl=", implode('|',array_keys($chart_pos)), 
         "&chco=00ff00&chtt=Positive+Balance\\\" alt=\\\"positive balance pie chart\\\" />";
}

echo "<br>";

if (sizeof($chart_neg) > 0)
{
    asort($chart_neg, SORT_NUMERIC);
    $chart_max = max($chart_neg);

    /* scale values from 0 to 100 */
    foreach ($chart_neg as &$value)
        $value *= 100 / $chart_max;

    /* shows pie chart image */
    echo "<img src=\\\"https://chart.googleapis.com/chart?chs=750x400&chd=t:",
         implode(',',$chart_neg), "&cht=p&chl=", implode('|',array_keys($chart_neg)), 
         "&chco=ff0000&chtt=Negative+Balance\\\" alt=\\\"negative balance pie chart\\\" />";
}

echo "\"'>Show balance charts</a></p>";
echo "</div>";

// Finaliza pagina
pagina_fim();
?>
